/**
 * This program is free software but WITHOUT ANY WARRANTY OR CONDITIONS OF ANY KIND.
 *
 * Copyright (C) 2008-2012 www.pretty-tools.com
 */

/**
 * Context Menu Listener.
 *
 * @author Alexander Kozlov
 */
function wsshContextMenuListener()
{
    this.logger = yrprxFactory.createLogger("wsshContextMenuListener");

    this.docURL = null;
    this.imageURL = null;
    this.image = null;
}

wsshContextMenuListener.prototype.onLoad = function(event)
{
    try
    {
        var listener = this;

        window.document.getElementById("contentAreaContextMenu").addEventListener("popupshowing", function(e){ listener.onPopupShowing(e); }, false);
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("onLoad: ", ex);
    }
}

wsshContextMenuListener.prototype.onUnLoad = function(event)
{
}

wsshContextMenuListener.prototype.onPopupShowing = function (event)
{
    if (!gContextMenu)
        return;

    try
    {
        var target = gContextMenu.target;
        var isImage = gContextMenu.onImage;

        this.docURL = gContextMenu.docURL;
        this.imageURL = gContextMenu.imageURL;
        this.image = isImage ? target : null;

        var slideShowItem = document.getElementById('WebSlideShowContextMenu_SlideShow');
        var fullScreenItem = document.getElementById('WebSlideShowContextMenu_FullScreen');
        var separatorBefore = document.getElementById('WebSlideShowContextMenu-separator-before');

        var count = g_wsshImageResolver.getImages(window.content.document, true);

        if (!isImage && !count)
        {
            slideShowItem.hidden = true;
            fullScreenItem.hidden = true;
        }
        else // show separators
        {
            slideShowItem.hidden = count < 2;
            fullScreenItem.hidden = !isImage;
        }

        var slideShowAllItem = document.getElementById('WebSlideShowContextMenu_SlideShow_All');
        slideShowAllItem.hidden = gBrowser.browsers.length <= 1;

        if (slideShowItem.hidden && fullScreenItem.hidden && slideShowAllItem.hidden)
            separatorBefore.hidden = true;
        else
        {
            if (separatorBefore && separatorBefore.previousSibling)
            {
                var prevVisible = separatorBefore.previousSibling;
                while (prevVisible && prevVisible.hidden)
                    prevVisible = prevVisible.previousSibling;

                separatorBefore.hidden = !prevVisible || prevVisible.tagName == "menuseparator";
            }
        }
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("onPopupShowing: ", ex);
    }
}

wsshContextMenuListener.prototype.onViewFullScreen = function(event)
{
    event.stopPropagation();

    try
    {
        var doc = window.content.document;

        var url = g_wsshImageResolver.getImageURL(doc, this.image);
        if (!url) url = this.imageURL;
        // show image full screen
        wsshWindowManager.openSlideShow([url], doc.title);
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("onViewFullScreen: ", ex);
    }
}

wsshContextMenuListener.prototype.onSlideShow = function(event)
{
    event.stopPropagation();

    try
    {
        var doc = window.content.document;

        var images = g_wsshImageResolver.getImages(doc);
        if (images && images.length)
            wsshWindowManager.openSlideShow(images, doc.title);
        else
            yrprxPrompt.alert(wsshResources["webslideshow.title"], wsshResources["webslideshow.can.not.find.appropriate.images"]);
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("onSlideShow: ", ex);
    }
}

wsshContextMenuListener.prototype.onSlideShowAll = function(event)
{
    event.stopPropagation();

    try
    {
        var images = new Array(), title = "";

        for (var i = 0, count = gBrowser.browsers.length; i < count; ++i)
        {
            var br = gBrowser.getBrowserAtIndex(i);

            var newImages = g_wsshImageResolver.getImages(br.contentDocument);
            if (newImages && newImages.length)
            {
                g_wsshImageResolver.removeDuplicates(newImages, images);

                if (title) title += ", ";
                title += br.contentDocument.title;
            }
        }

        if (images && images.length)
            wsshWindowManager.openSlideShow(images, title);
        else
            yrprxPrompt.alert(wsshResources["webslideshow.title"], wsshResources["webslideshow.can.not.find.appropriate.images"]);
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("onSlideShow: ", ex);
    }
}
